//
//  TSWithDrawViewController.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSWithDrawViewController: TSBaseViewController {
    
    var tableView : UITableView!
    var headerView : TSWithDrawHeaderView!
    var dataSource : [TSNormalCellModel] = []
    var cardModel : TSCardListModel? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()

        handleDatas()
        configViews()
        setupNavigationTitle(title: "提现")
//        requestCardListDatas()
    }
    
    func handleDatas() {
        
        let titleArray = ["盈利钱包提现", "中心钱包提现"]
        
        for str in titleArray {
            let model = TSNormalCellModel.init(title: str, value: "", textColor: "#282828", font: UIFont.systemFont(ofSize: 14, weight: .medium))
            dataSource.append(model)
        }
    }

}

//MARK: - TSCardBindViewControllerDelegate
extension TSWithDrawViewController : TSCardBindViewControllerDelegate {
    
    func completeBindCard() {
        self.requestCardListDatas()
    }
}

//MARK: - TSWithDrawBankViewControllerDelegate
extension TSWithDrawViewController : TSWithDrawBankViewControllerDelegate {
    
    func bankCompleteWithdraw() {
        self.headerView.refreshHeaderView()
    }
}

//MARK: - TSWithDrawUSDTViewController
extension TSWithDrawViewController : TSWithDrawUSDTViewControllerDelegate {
    
    func completeWithdraw() {
        self.headerView.refreshHeaderView()
    }
}

//MARK: - UITableViewDelegate
extension TSWithDrawViewController : UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if indexPath.section != 0 {
            return
        }
        
        let vc = TSWithDrawUSDTViewController()
        vc.delegate = self
        vc.withdrawType = indexPath.row
         self.navigationController?.pushViewController(vc, animated: true)

//        let alertVC = UIAlertController.init(title: "提现方式", message: nil, preferredStyle: .actionSheet)
//        let bankAlert = UIAlertAction.init(title: "银行卡", style: .default) { (UIAlertAction) in
//
//            if self.cardModel == nil {
//                let alertVC1 = UIAlertController.init(title: "您还没有绑定银行卡，请先绑定银行卡", message: nil, preferredStyle: .alert)
//                let cardCancel = type(of: UIAlertAction).init(title: "取消", style: .default, handler: nil)
//                let cardBind = type(of: UIAlertAction).init(title: "去绑定", style: .destructive) { (UIAlertAction) in
//                    let vc = TSCardBindViewController.init()
//                    vc.delegate = self
//                    self.navigationController?.pushViewController(vc, animated: true)
//                }
//                alertVC1.addAction(cardCancel)
//                alertVC1.addAction(cardBind)
//                self.present(alertVC1, animated: true, completion: nil)
//                return
//            }
//
//            let vc = TSWithDrawBankViewController()
//            vc.model = self.cardModel!
//            vc.delegate = self
//            vc.withdrawType = indexPath.row
//            self.navigationController?.pushViewController(vc, animated: true)
//        }
//
//        let usdtAlert = UIAlertAction.init(title: "USDT", style: .default) { (UIAlertAction) in
//
//
//           let vc = TSWithDrawUSDTViewController()
//           vc.delegate = self
//           vc.withdrawType = indexPath.row
//            self.navigationController?.pushViewController(vc, animated: true)
//        }
//        let cancelAlert = UIAlertAction.init(title: "取消", style: .destructive) { (UIAlertAction) in
//            self.dismiss(animated: true, completion: nil)
//        }
//        alertVC.addAction(bankAlert)
//        alertVC.addAction(usdtAlert)
//        alertVC.addAction(cancelAlert)
//        self.present(alertVC, animated: true, completion: nil)
    }
}

//MARK: - UITableViewDataSource
extension TSWithDrawViewController : UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 0 ? dataSource.count : 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell : UITableViewCell?
        
        if indexPath.section == 0 {
            var normalCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(TSNormalTableViewCell.self)) as? TSNormalTableViewCell
            
            if normalCell == nil {
                normalCell = TSNormalTableViewCell.init(style: .default, reuseIdentifier: NSStringFromClass(TSNormalTableViewCell.self))
            }
            
            normalCell?.setupCell(model: dataSource[indexPath.row], last: false)
            
            cell = normalCell
            
        } else {
            var autoHeightCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(TSAutoHeightTableViewCell.self)) as? TSAutoHeightTableViewCell
            
            if autoHeightCell == nil {
                autoHeightCell = TSAutoHeightTableViewCell.init(style: .default, reuseIdentifier: NSStringFromClass(TSAutoHeightTableViewCell.self))
            }
            
            autoHeightCell?.setupLabel(content: getInfoContent(), topInset: 30, textColor: "#686868", fontSize: 13)
            
            cell = autoHeightCell
        }
        
        return cell!
    }
}

// MARK: - Network Request
extension TSWithDrawViewController {
    
    func requestCardListDatas() {
        
        self.cardModel = nil
        
        CBToast.showToastAction()
        TSNetworkManager.request(TMSNetworkTools.getCardList, showHUD: false, success: { (data) in
            
            CBToast.hiddenToastAction()
            let array = data as? Array<Dictionary<String, Any>>
            if array?.count ?? 0 > 0 {
                for item in array! {
                    let tempModel = TSCardListModel.deserialize(from: item)
                    if let model = tempModel {
                        self.cardModel = model
                        break
                    }
                }
            }
            
        }) { (error) in
            
            CBToast.hiddenToastAction()
        }
    }
}

//MARK: - Initialize
extension TSWithDrawViewController {
    
    func configViews() {
        
        headerView = TSWithDrawHeaderView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 228))
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = WHITE_COLOR
        tableView.delegate = self;
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableView.automaticDimension
        tableView.tableHeaderView = headerView
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(T_NAVBARHEIGHT)
        }

        if #available(iOS 11, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
    }
    
    func getInfoContent() -> String {
        return "温馨提示：\n1 出款需达到兑换金币30%流水；\n2 盈利钱包提现无手续费，中心钱包提现手续费10%；\n3 USDT提现无上限。"
    }
}

